<!--
文件名：ParentView.vue
作者：系统自动生成
创建日期：2025-04-27
描述：家长管理视图组件，包含家长信息展示和学生关联管理功能
-->
<template>
  <div class="parent-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>家长信息管理</span>
      </div>
      
      <!-- 家长信息展示区域 -->
      <el-table
        :data="parentList"
        style="width: 100%"
        border
        stripe
      >
        <el-table-column
          prop="name"
          label="家长姓名"
          width="120"
        ></el-table-column>
        <el-table-column
          prop="phone"
          label="联系电话"
          width="150"
        ></el-table-column>
        <el-table-column
          prop="email"
          label="电子邮箱"
          width="200"
        ></el-table-column>
        <el-table-column
          prop="relationship"
          label="与学生关系"
          width="120"
        ></el-table-column>
        <el-table-column
          label="关联学生"
          width="300"
        >
          <template slot-scope="scope">
            <el-tag 
              v-for="student in scope.row.students" 
              :key="student.id"
              type="info"
              style="margin-right: 5px;"
            >
              {{ student.name }} ({{ student.grade }})
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="180"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.row)"
            >编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <!-- 分页控件 -->
      <div class="pagination-container">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </el-card>
    
    <!-- 学生关联管理对话框 -->
    <el-dialog
      title="关联学生管理"
      :visible.sync="dialogVisible"
      width="50%"
    >
      <el-transfer
        v-model="selectedStudents"
        :data="allStudents"
        :titles="['可选学生', '已关联学生']"
        :props="{
          key: 'id',
          label: 'name'
        }"
      ></el-transfer>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveStudentRelation">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getParents, getStudents, updateStudentRelation } from '@/api/parent'

export default {
  name: 'ParentView',
  data() {
    return {
      parentList: [], // 家长列表数据
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页显示条数
      total: 0, // 总记录数
      dialogVisible: false, // 关联学生对话框显示状态
      allStudents: [], // 所有学生数据
      selectedStudents: [], // 已选择的学生ID列表
      currentParentId: null // 当前操作的家长ID
    }
  },
  created() {
    this.fetchParents()
    this.fetchAllStudents()
  },
  methods: {
    // 获取家长列表
    fetchParents() {
      const params = {
        page: this.currentPage,
        size: this.pageSize
      }
      getParents(params).then(response => {
        this.parentList = response.data.list
        this.total = response.data.total
      })
    },
    // 获取所有学生列表
    fetchAllStudents() {
      getStudents().then(response => {
        this.allStudents = response.data
      })
    },
    // 编辑家长信息
    handleEdit(row) {
      this.currentParentId = row.id
      this.selectedStudents = row.students.map(item => item.id)
      this.dialogVisible = true
    },
    // 删除家长
    handleDelete(row) {
      this.$confirm('确认删除该家长信息吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 调用删除API
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })        
      })
    },
    // 保存学生关联关系
    saveStudentRelation() {
      const params = {
        parentId: this.currentParentId,
        studentIds: this.selectedStudents
      }
      updateStudentRelation(params).then(() => {
        this.$message.success('保存成功')
        this.dialogVisible = false
        this.fetchParents() // 刷新家长列表
      })
    },
    // 每页条数变化
    handleSizeChange(val) {
      this.pageSize = val
      this.fetchParents()
    },
    // 当前页变化
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchParents()
    }
  }
}
</script>

<style scoped>
.parent-container {
  padding: 20px;
}
.pagination-container {
  margin-top: 20px;
  text-align: right;
}
</style>